#!/bin/bash

# 文语编程语言 CLI 包装脚本
# 支持 Linux/Mac 系统

# 获取脚本所在目录
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PROJECT_ROOT="$SCRIPT_DIR"
CLI_PATH="$PROJECT_ROOT/src/WenYu.CLI/WenYu.CLI.csproj"

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color

# 如果未提供命令，显示帮助
if [ $# -eq 0 ]; then
    dotnet run --project "$CLI_PATH" -- --help
    exit $?
fi

COMMAND="$1"
FILE="$2"

case "${COMMAND,,}" in
    run)
        if [ -z "$FILE" ]; then
            echo -e "${RED}❌ 错误: 缺少文件参数${NC}"
            echo "用法: wenyu run <file>"
            exit 1
        fi
        
        # 检查文件是否存在
        if [ ! -f "$FILE" ]; then
            echo -e "${RED}❌ 错误: 文件不存在: $FILE${NC}"
            exit 1
        fi
        
        # 获取绝对路径
        ABSOLUTE_PATH="$(cd "$(dirname "$FILE")" && pwd)/$(basename "$FILE")"
        
        echo -e "${CYAN}📂 运行: $FILE${NC}"
        dotnet run --project "$CLI_PATH" -- run "$ABSOLUTE_PATH"
        exit $?
        ;;
        
    compile)
        if [ -z "$FILE" ]; then
            echo -e "${RED}❌ 错误: 缺少文件参数${NC}"
            echo "用法: wenyu compile <file>"
            exit 1
        fi
        
        # 检查文件是否存在
        if [ ! -f "$FILE" ]; then
            echo -e "${RED}❌ 错误: 文件不存在: $FILE${NC}"
            exit 1
        fi
        
        # 获取绝对路径
        ABSOLUTE_PATH="$(cd "$(dirname "$FILE")" && pwd)/$(basename "$FILE")"
        
        echo -e "${CYAN}📂 编译: $FILE${NC}"
        dotnet run --project "$CLI_PATH" -- compile "$ABSOLUTE_PATH"
        exit $?
        ;;
        
    --version)
        dotnet run --project "$CLI_PATH" -- --version
        exit $?
        ;;
        
    --help|-h)
        dotnet run --project "$CLI_PATH" -- --help
        exit $?
        ;;
        
    *)
        echo -e "${RED}❌ 未知命令: $COMMAND${NC}"
        echo ""
        dotnet run --project "$CLI_PATH" -- --help
        exit 1
        ;;
esac
